package com.business.section.controller;

import java.io.File;
import java.util.Date;

import com.business.section.validator.SectionValidator;
import com.common.util.picture.PictureBase64;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.Section;
import com.model.User;

/**
 * 
 * @author qing
 */
public class SectionController extends Controller {
	
	
	public void add() {
	}

	//新增
	//@Before(SectionValidator.class)
	public void save() {
		getFile("contextFile");
		User user=(User) getSession().getAttribute("user");
		//Section section = getModel(Section.class);
		Section section = new Section();
		section.setName(getPara("name"));
		section.setSectionImg(getPara("section_img"));
		section.setContext(getPara("context"));
		@SuppressWarnings("deprecation")
		String context=PictureBase64.getShtmlContent(section.getContext(),
				PropKit.get("uploadFileUrl")+File.separator+"context",
				getRequest().getRealPath("/"),getRequest().getContextPath());
		if(section.getSectionImg()!=null&&!"".equals(section.getSectionImg().trim())){
			@SuppressWarnings("deprecation")
			String imgurl = PictureBase64.getImgUrl(section.getSectionImg(), 
					PropKit.get("uploadFileUrl")+File.separator+"context",
					getRequest().getRealPath("/"),getRequest().getContextPath());
			section.setSectionImg(imgurl);
		}
		String text = PictureBase64.removeHtmlInTag(context);
		String rmark = "";
		if(text.length()>100)
			rmark = text.substring(0,100)+"...";
		else
			rmark = text.substring(0,text.length())+"...";
		section.setRemark(rmark);
		//Section section=new Section();
		//section.setName(getPara("name"));
		section.setUserId(user.getId());
		section.setContext(context);
		section.setUpdateTime(new Date());
		section.setItStatus(1);
		section.setCreateTime(new Date());
		section.save();
		
		//做等级统计
		user.setIntegral(user.getIntegral()+5);//发帖经验+5
		if(user.getUserLevel()>1){
			if(user.getIntegral()>=(user.getUserLevel()-1)*100){
				user.setIntegral(user.getIntegral()-(user.getUserLevel()-1)*100);
				user.setUserLevel(user.getUserLevel()+1);
			}
		}else{
			if(user.getUserLevel()>0){
				if(user.getIntegral()>=50){
					user.setIntegral(user.getIntegral()-50);
					user.setUserLevel(user.getUserLevel()+1);
				}
			}else{
				if(user.getIntegral()>=10){
					user.setIntegral(user.getIntegral()-10);
					user.setUserLevel(user.getUserLevel()+1);
				}
			}
		}
		user.update();
		redirect("/");
	}

	// 删除 3
	public void delete() {
		Section section = Section.dao.findById(getParaToInt());
		section.setItStatus(3);
		section.update();
		redirect("/section");
	}
	// 结贴 2
	public void over() {
		Section section = Section.dao.findById(getParaToInt());
		section.setItStatus(2);
		section.update();
		redirect("/section");
	}
	
	//修改
	public void edit(){
		Section section = Section.dao.findById(getParaToInt());
		User user = (User) getSession().getAttribute("user");
		if(section==null||user==null||section.getUserId().intValue()!=user.getId().intValue()){
			redirect("/");
		}
		setAttr("section",section);
	}
	
	//修改保存
	@Before(SectionValidator.class)
	public void update(){
		getFile("contextFile");
		Section section = getModel(Section.class);
		User user = (User) getSession().getAttribute("user");
		Section sectionUpdate = Section.dao.findById(getParaToInt());
		if(user==null){
			redirect("/login");
			return;
		}
		@SuppressWarnings("deprecation")
		String context=PictureBase64.getShtmlContent(section.getContext(),
				PropKit.get("uploadFileUrl")+File.separator+"context",
				getRequest().getRealPath("/"),getRequest().getContextPath());
		if(section.getSectionImg()!=null&&!"".equals(section.getSectionImg().trim())){
			@SuppressWarnings("deprecation")
			String imgurl = PictureBase64.getImgUrl(section.getSectionImg(), 
					PropKit.get("uploadFileUrl")+File.separator+"context",
					getRequest().getRealPath("/"),getRequest().getContextPath());
			sectionUpdate.setSectionImg(imgurl);
		}
		String text = PictureBase64.removeHtmlInTag(context);
		String rmark = "";
		if(text.length()>100)
			rmark = text.substring(0,100)+"...";
		else
			rmark = text.substring(0,text.length())+"...";
		
		sectionUpdate.setRemark(rmark);
		sectionUpdate.setContext(context);
		sectionUpdate.setUpdateTime(new Date());
		sectionUpdate.update();
		redirect("/");
	}
	
	//列表
	public void index() {  
		User user = (User) getSession().getAttribute("user");
		if(user == null){
			redirect("/");
		}else{
			String keyword = (String) getSession().getAttribute("keyword");
			String sqloption = "SELECT `user`.id AS 'user.id',`user`.nick_name AS 'user.nickName', "
					+ "`user`.head_portrait_img AS 'user.headPortraitImg',"
					+ "`section`.`id` AS 'section.id',`section`.`name` AS 'section.name',"
					+ "`section`.remark AS 'section.remark',"
					+ "`section`.it_status AS 'section.itStatus',"
					+ "`section`.section_img AS 'section.sectionImg'";
			String sql = " FROM "
					+ "`user` INNER JOIN "
					+ "`section` ON(`section`.user_id = `user`.id) WHERE `user`.id="+user.getId()+" AND `section`.it_status<3 ";
			if(keyword!=null&&!"".equals(keyword.trim())){
				sql = sql +" AND `section`.context LIKE \"%"+keyword.trim()+"%\" OR `section`.name LIKE \"%"+keyword.trim()+"%\" ";
			}
			sql = sql + " ORDER BY `section`.id desc";
			Page<Record> userAndSectionRecord = Db.paginate(getParaToInt(0,1), 5,sqloption, sql);
			setAttr("sectionPage",userAndSectionRecord);
	        render("section.html");   
		}
    }
}
